package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.AssetDetailQueryDto;
import com.group.eams.pojo.dto.AssetReportQueryDto;
import com.group.eams.pojo.entity.AssetReport;
import com.group.eams.pojo.vo.AssetReportListVo;
import com.group.eams.pojo.vo.FixedAssetListVo;
import com.group.eams.service.AssetReportService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产报修(AssetReport)表控制层
 *
 * @author lqk
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/assetReport")
@Api(tags = "资产报修表相关接口")
public class AssetReportController {
    /**
     * 服务对象
     */
    @Autowired
    private AssetReportService assetReportService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "资产报修列表")
    public Result list(AssetReportQueryDto assetReportQueryDto) {
        //1.准备分页条件
        Page<AssetReport> page = new Page<>(assetReportQueryDto.getPage(), assetReportQueryDto.getLimit());

        //2.设置查询条件
        Page<AssetReportListVo> assetReportByPage = assetReportService.findAssetReportByPage(page, assetReportQueryDto);

        return Result.buildSuccess(assetReportByPage.getTotal(), assetReportByPage.getRecords());
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找资产报修")
    public Result find(@RequestParam Long id) {
        AssetReport assetReport = assetReportService.getById(id);
        return Result.buildSuccess(assetReport);
    }

    @RequestMapping(value = "/findByAssetReportNum", method = RequestMethod.GET)
    @ApiOperation(value = "根据单号查找资产报修")
    public Result findByAssetReportNum(@RequestParam String assetReportNum) {
        List<AssetReport> assetReport = assetReportService.findByAssetReportNum(assetReportNum);
        return Result.buildSuccess(assetReport);
    }

    @RequestMapping(value = "/findDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查找资产报修详细")
    public Result findDetail( AssetDetailQueryDto assetDetailQueryDto) {
        //设置查询单号
        assetDetailQueryDto.setAssetReportNum(assetDetailQueryDto.getAssetReportNum());
        Page<AssetReport> page = new Page<>(assetDetailQueryDto.getPage(), assetDetailQueryDto.getLimit());
        Page<FixedAssetListVo> assetReportDetailByPage =
                assetReportService.findAssetReportDetailByPage(page, assetDetailQueryDto);
        return Result.buildSuccess(assetReportDetailByPage.getTotal(), assetReportDetailByPage.getRecords());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加资产报修")
    public Result add(@RequestBody AssetReport assetReport) {
        boolean result = assetReportService.save(assetReport);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改资产报修")
    public Result update(@RequestBody AssetReport assetReport) {
        boolean result = assetReportService.updateById(assetReport);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation("删除资产报修")
    public Result delete(@RequestParam Long id) {
        boolean result = assetReportService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove", method = RequestMethod.POST)
    @ApiOperation("批量删除资产报修")
    public Result batchDelete(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = assetReportService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation("查询所有资产报修")
    public Result findAll() {
        List<AssetReport> assetReportList = assetReportService.list();
        return Result.buildSuccess(assetReportList);
    }

    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ApiOperation("批量添加资产报修")
    public Result batchAdd(@RequestBody List<AssetReport> assetReports) {
        boolean result = assetReportService.saveBatch(assetReports);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation("批量修改资产报修")
    public Result batchUpdate(@RequestBody List<AssetReport> assetReports) {
        boolean result = assetReportService.updateBatchById(assetReports);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}
